import config from 'config'; // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
import superagent from 'superagent';

// order docs https://sequelize.org/docs/v6/core-concepts/model-querying-basics/
class ReservationController {
  async findAll(req, res, next) {
    const service = config.get('localService');
    const { hotel_id } = req.user;

    try {
      const response = await superagent.post(service + '/remote-call')
        .set('Content-Type', 'application/json')
        .accept('application/json')
        .send({
          hotelId: hotel_id,
          api: `reservation/future-booking-summary`,
          method: 'post',
          ...(req.query)
        });

      if (response.status === 200) {
        if (response.body.errCode === 0) {
          res.send(response.body);
        } else {
          throw new Error(response.body.errMsg);
        }
      } else {
        throw new Error(response.text);
      }
    }
    catch (e) {
      next(e);
    }
  }
}

export default new ReservationController();
